#include "TLineEdit.h"

TLineEdit::TLineEdit(QWidget *parent) :
    QLineEdit(parent)
{
}

void TLineEdit::setNullStr(const QString &text)
{
  if (_nullStr == text )
    return;

  _nullStr = text;
  sHandleNullStr();
  if (!_nullStr.isEmpty())
    connect(this, SIGNAL(textChanged(QString)), this, SLOT(sHandleNullStr()));
}

void TLineEdit::focusInEvent(QFocusEvent * event)
{
  if (!_nullStr.isEmpty())
  {
    disconnect(this, SIGNAL(textChanged(QString)), this, SLOT(sHandleNullStr()));
    sHandleNullStr();
  }
  QLineEdit::focusInEvent(event);
}

void TLineEdit::focusOutEvent(QFocusEvent * event)
{
  if (!_nullStr.isEmpty())
  {
    connect(this, SIGNAL(textChanged(QString)), this, SLOT(sHandleNullStr()));
    sHandleNullStr();
  }
  QLineEdit::focusOutEvent(event);
}

void TLineEdit::sHandleNullStr()
{
  if (_nullStr.isEmpty())
    return;

  QString sheet = styleSheet();
  QString nullStyle = " QLineEdit{ color: Grey; "
                      "            font: italic; "
                      "            font-size: 12px}";

  if (!hasFocus() &&
      text().isEmpty())
  {
    setText(_nullStr);
    sheet.append(nullStyle);
    _isNull = true;
  }
  else if (hasFocus() &&
           sheet.contains(nullStyle))
  {
    clear();
    sheet.remove(nullStyle);
  }
  else
  {
    _isNull = false;
    sheet.remove(nullStyle);
  }

  setStyleSheet(sheet);
}
